package com.tutu.service.impl;

import com.tutu.controller.backend.SpecialGroupController;
import com.tutu.core.api.SpecialGroupApiUrls;
import com.tutu.core.api.SpecialGroupUserApiUrls;
import com.tutu.service.SpecialGroupService;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.JsonUtil;
import com.tutu.utils.UnicodeUtil;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class SpecialGroupServiceImpl implements SpecialGroupService {

    @Override
    public Map<String, Object> pageByParams(String keyWord, Integer pageNum, Integer pageSize) {
        Map<String, Object> dataMap = HttpRequestUtil.getOnlyData(SpecialGroupApiUrls.base_prefix + SpecialGroupApiUrls.page, "keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);

        Map<String, Object> map;
        SpecialGroupController.tempSpecialGroupMap.clear();

        for (Object o : JSONArray.fromObject(dataMap.get("content"))) {
            map = JsonUtil.json2Map(o);

            SpecialGroupController.tempSpecialGroupMap.put(map.get("id") + "", map);
        }

        return dataMap;
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer companyId, String content, Integer turnIndex) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑特别分组信息，公司ID：" + companyId + "，分组名称：" + content + "，排序编号：" + turnIndex);

        return HttpRequestUtil.getCompleteData(SpecialGroupApiUrls.base_prefix + SpecialGroupApiUrls.update, "content=" + UnicodeUtil.unicodeEncode(content) + "&turnIndex=" + turnIndex + "&companyId=" + companyId);
    }

    @Override
    public Map<String, Object> delInfo(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除特别分组信息，ID：" + id);

        return HttpRequestUtil.getCompleteData(SpecialGroupApiUrls.base_prefix + SpecialGroupApiUrls.delete, "id=" + id);
    }

    @Override
    public Map<String, Object> childPageByParams(Integer specialGroupId, String keyWord, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(SpecialGroupApiUrls.base_prefix + SpecialGroupApiUrls.childPage, "specialGroupId=" + specialGroupId + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);
    }

    @Override
    public Map<String, Object> saveChildInfo(HttpServletRequest request, Integer specialGroupId, Integer deviceId) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑特别分组信息，ID：" + specialGroupId + "，设备ID：" + deviceId);

        return HttpRequestUtil.getCompleteData(SpecialGroupApiUrls.base_prefix + SpecialGroupApiUrls.childUpdate, "specialGroupId=" + specialGroupId + "&deviceId=" + deviceId);
    }

    @Override
    public Map<String, Object> delChildInfo(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除特别分组信息，ID：" + id);

        return HttpRequestUtil.getCompleteData(SpecialGroupApiUrls.base_prefix + SpecialGroupApiUrls.childDelete, "id=" + id);
    }

    @Override
    public Map<String, Object> childUserPageByParams(Integer specialGroupId, String keyWord, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(SpecialGroupUserApiUrls.base_prefix + SpecialGroupUserApiUrls.page, "specialGroupId=" + specialGroupId + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);
    }

    @Override
    public Map<String, Object> saveChildUserInfo(HttpServletRequest request, Integer specialGroupId, Integer userId) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑特别分组用户关联信息，ID：" + specialGroupId + "，用户ID：" + userId);

        return HttpRequestUtil.getCompleteData(SpecialGroupUserApiUrls.base_prefix + SpecialGroupUserApiUrls.update, "specialGroupId=" + specialGroupId + "&userId=" + userId);
    }

    @Override
    public Map<String, Object> delChildUserInfo(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除特别分组用户关联信息，ID：" + id);

        return HttpRequestUtil.getCompleteData(SpecialGroupUserApiUrls.base_prefix + SpecialGroupUserApiUrls.delete, "id=" + id);
    }
}